/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package excom;


import bus.ContactBUS;
import dto.ContactDTO;
import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.TableColumn;
import ui.MainForm;

/**
 *
 * @author chanhpm
 */

public class OutlookContact extends JPanel{

     private JPanel header;
     private JLabel title;
     private JPanel body;
     private JPanel footer;
     private JTable listOfContact;
    // protected TableColumn ColumnHide;
     private XTableModel tableModel ;
     private int[] contactId;
     public  OutlookContact(){
         this.setLayout(new BorderLayout());         
         initComponents();
     }


    /**
     * khoi tao component
     */
     public void initComponents()
     {
         createHeader();
         createBody();
         createFooter();
     }

     /**
      * tao header
      */
     private void createHeader()
     {
          header = new JPanel(new BorderLayout());
          title = new JLabel("Contact");
          title.setFont(new Font("Arial",Font.BOLD,18));
          title.setForeground(Color.BLUE);
          title.setIcon(new ImageIcon(MainForm.ResourceDir + "contact.png"));
          header.add(title, BorderLayout.WEST);
          this.add(header, BorderLayout.NORTH);
     }

     /**
      * tao phan than
      */
     private void createBody()
     {
         body = new JPanel(new BorderLayout());         
         String[] columnName = {"Name","Company","Job","Email","Display As","Webpage","IMAdress"};
         Object[] obj = {"","","","","","",""};
         Vector temp = new Vector();
         temp.add(obj);
         
         tableModel = new XTableModel(temp, columnName);

         listOfContact = new JTable( tableModel);
         listOfContact.setRowHeight(30);
         listOfContact.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
         JScrollPane scrollPane = new JScrollPane(listOfContact);
         listOfContact.setFillsViewportHeight(true);

         body.add(scrollPane, BorderLayout.CENTER);
         this.add(body, BorderLayout.CENTER);
         refresh();
     }

     /**
      * tao footer
      */
     private void createFooter(){
         footer = new JPanel(new BorderLayout());
         this.add(footer, BorderLayout.SOUTH);
     }
     /**
      * Doi che do xem mail: mail den, mail di, mail nhap
      * @param modeinquire
      * @param showText
      */
     public void inquire(){
         title.setText("Contact");        
     }

 /*    private void clearAllRow(){
          while(model.getRowCount() > 0) {
           model.removeRow(0);
        }
     }*/

     public void refresh(){
        tableModel.clearallRow();
        ArrayList<ContactDTO> list = new ArrayList<ContactDTO>();
        ContactBUS bus = new ContactBUS();
        try {
            list = bus.getAllRecords();
            Vector v = new Vector();
            contactId = new int[list.size()];
            for(int i = 0; i < list.size(); i++)
            {
                ContactDTO c = list.get(i);
                Object[] temp = {c.getFullName(),c.getCompany(),
                                c.getJobTitle(),c.getEmail(),
                                c.getDisplayAs(), c.getWebAddress(), c.getIMAddress()};
                v.add(temp);
                contactId[i] = c.getId();
            }

            if(v.size() > 0)
                tableModel.fillData(v);
            
        } catch (SQLException ex) {
           ex.printStackTrace();
        }
       
     }
     public int getContactIDSelected(){
        int rowselected = listOfContact.getSelectedRow();
        if(rowselected < 0)
            return -1;
        return contactId[rowselected];
     }
}
